#!/usr/bin/python

import glob
import json
import pprint
import hgbfe_dcardgen

from sys import argv
from hgbfe.model import model

def getModelsDict(fileList):
    pp = pprint.PrettyPrinter(indent=4)

    modelsDict = {}
    for file in fileList:
        print "Reading file: %s" % (file)
        fp = open(file)        
        jsonObj = json.load(fp) 
        #pp.pprint(jsonObj)        
        modelObj = model(jsonObj)
        #print "Adding model named: %s" % modelObj.name
        modelsDict[modelObj.name] = modelObj
        #for weapon in modelObj.weapons_ranged:
        #    print "hgbfe_generate:Found weapon: %s" % weapon.name

    return modelsDict
    
def printModels(modelsDict):    
    valueL = sorted(modelsDict.values(),  modelCmp)
    
    # Determine the faction
    factionS = 'default'
    if argv[1].find('north') != -1:
        factionS = 'north'
    elif argv[1].find('south') != -1:
        factionS = 'south'
    elif argv[1].find('paxton') != -1:
        factionS = 'paxton'
    elif argv[1].find('pak') != -1:
        factionS = 'pak'
    elif argv[1].find('cef') != -1:
        factionS = 'cef'
    elif argv[1].find('caprice') != -1:
        factionS = 'caprice'
    elif argv[1].find('talons') != -1:
        factionS = 'talons'
    
    hgbfe_dcardgen.drawTest(valueL,  factionS)
    return
    
def modelCmp(x,  y):
    if isinstance(x,  model): 
        if x.type == "gear":
            if y.type != "gear":
                return -1
        elif x.type == "vehicle":
            if y.type == "gear":
                return 1
            if y.type != x.type:
                return -1
        elif x.type == "strider":        
            if y.type == "gear" or y.type == "vehicle":
                return 1
            if y.type != x.type:
                return -1
        elif x.type == "infantry":
            if y.type == "gear" or y.type == "vehicle" or y.type == "strider":
                return 1
            if y.type != x.type:
                return -1
        elif x.type == "aircraft":
            if y.type != x.type and y.type != "structure":
                return 1
        elif x.type == "structure":
            if y.type != "structure":
                return 1
        
        if x.chassis != y.chassis:
            #print "Chassis differ [%s/%s] v. [%s/%s]" % (x.name,  x.chassis,  y.name,  y.chassis)
            return cmp(x.chassis.lower(),  y.chassis.lower())
        else:
            if (x.name.lower() ==  x.chassis.lower()):
                #print "Base model [%s],  should be first!" % x.name
                return -1
            elif (y.name.lower() == y.chassis.lower()):
                #print "Base model [%s],  should be first!" % y.name
                return 1
            else:
                #print "Chassis match, matching [%s/%s] v. [%s/%s] on name" % (x.name,  x.chassis,  y.name,  y.chassis)
                return cmp(x.name.lower(),  y.name.lower())        
    else:
        return cmp(x,  y)

if __name__ == '__main__':
    
    targetDir = "hgbfe/test"
    if (len(argv) == 2):
       targetDir = argv[1]
    targetS = targetDir + '/*.json'
    files = glob.iglob(targetS)

    modelsDict = getModelsDict(files)
    printModels(modelsDict) 
